import { unref, isRef } from 'vue';
import { useDefaultEnhanced } from '../hooks/useJVxeComponent';
import { isFunction, isObject, isString } from '/@/utils/is';
import { componentMap } from '../componentMap';
// 已注册的组件增强
const enhancedMap = new Map();
/**
 * 获取某个组件的增强
 * @param type JVxeTypes
 */
export function getEnhanced(type) {
    let $type = type;
    if (!enhancedMap.has($type)) {
        let defaultEnhanced = useDefaultEnhanced();
        if (componentMap.has($type)) {
            let enhanced = componentMap.get($type)?.enhanced ?? {};
            if (isObject(enhanced)) {
                Object.keys(defaultEnhanced).forEach((key) => {
                    let def = defaultEnhanced[key];
                    if (enhanced.hasOwnProperty(key)) {
                        // 方法如果存在就不覆盖
                        if (!isFunction(def) && !isString(def)) {
                            enhanced[key] = Object.assign({}, def, enhanced[key]);
                        }
                    }
                    else {
                        enhanced[key] = def;
                    }
                });
                enhancedMap.set($type, enhanced);
                return enhanced;
            }
        }
        else {
            throw new Error(`[JVxeTable] ${$type} 组件尚未注册，获取增强失败`);
        }
        enhancedMap.set($type, defaultEnhanced);
    }
    return enhancedMap.get($type);
}
/** 辅助方法：替换${...}变量 */
export function replaceProps(col, value) {
    if (value && typeof value === 'string') {
        let text = value;
        text = text.replace(/\${title}/g, col.title);
        text = text.replace(/\${key}/g, col.key);
        text = text.replace(/\${defaultValue}/g, col.defaultValue);
        return text;
    }
    return value;
}
/** 模拟触发事件 */
export function dispatchEvent(options) {
    const { props, $event, row, column, instance, className, handler, isClick } = options;
    if ((!$event || !$event.path) && !instance) {
        return;
    }
    // alwaysEdit 下不模拟触发事件，否者会导致触发两次
    if (props && props.alwaysEdit) {
        return;
    }
    let getCell = () => {
        let paths = [...($event?.path ?? [])];
        // 通过 instance 获取 cell dom对象
        if (row && column) {
            let selector = `table.vxe-table--body tbody tr[rowid='${row.id}'] td[colid='${column.id}']`;
            let cellDom = instance.vnode?.el?.querySelector(selector);
            if (cellDom) {
                paths.unshift(cellDom);
            }
        }
        for (const el of paths) {
            if (el.classList?.contains('vxe-body--column')) {
                return el;
            }
        }
        return null;
    };
    let cell = getCell();
    if (cell) {
        window.setTimeout(() => {
            let getElement = () => {
                let classList = className.split(' ');
                if (classList.length > 0) {
                    const getClassName = (cls) => {
                        if (cls.startsWith('.')) {
                            return cls.substring(1, cls.length);
                        }
                        return cls;
                    };
                    let get = (target, className, idx = 0) => {
                        let elements = target.getElementsByClassName(getClassName(className));
                        if (elements && elements.length > 0) {
                            return elements[idx];
                        }
                        return null;
                    };
                    let element = get(cell, classList[0]);
                    for (let i = 1; i < classList.length; i++) {
                        if (!element) {
                            break;
                        }
                        element = get(element, classList[i]);
                    }
                    return element;
                }
                return null;
            };
            let element = getElement();
            if (element) {
                if (isFunction(handler)) {
                    handler(element);
                }
                else {
                    // 模拟触发点击事件
                    if (isClick) {
                        element.click();
                    }
                    else {
                        element.dispatchEvent($event);
                    }
                }
            }
        }, 10);
    }
    else {
        console.warn('【JVxeTable】dispatchEvent 获取 cell 失败');
    }
}
/** 绑定 VxeTable 数据 */
export function vModel(value, row, column) {
    let property = isRef(column) ? column.value.property : column;
    unref(row)[property] = value;
}
